<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/28 0028
 * Time: 上午 9:20
 */

namespace app\sports_admin\service;

use app\exception\SportsGameException;
use app\sports_admin\model\SportsGame as SportsGameModel;
class SportsGame extends Base
{
    /**
     * 运行游戏列表
     */
    public static function sportsGameList()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        //第几页
        $params['page'] = intval($params['page']);
        //每页条数
        $params['rows'] = intval($params['rows']);
        $info = SportsGameModel::sportsGameList($params);
        return json($info);
    }
    /**
     * 运动游戏添加
     */
    public static function insert()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = SportsGameModel::create([
            'title'=>$params['title'],'contents'=>$params['contents'],'age_group'=>$params['min'].'-'.$params['max'],
            'images'=>ltrim($params['images'],'/'),'video'=>$params['video'],'target'=>$params['target']
        ]);
        if(!$bool){
            throw new SportsGameException(['msg'=>'添加失败','errorCode'=>400]);
        }
        return json(['code'=>200,'msg'=>'添加成功']);
    }
    /**
     * 删除
     */
    public static function delete()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = SportsGameModel::destroy($params['id']);
        if(!$bool){
            throw new SportsGameException(['msg'=>'删除失败','errorCode'=>400]);
        }
        return json(['code'=>200,'msg'=>'删除成功']);
    }
}